package com.amumu.netctoss.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.amumu.netctoss.dao.AdminDao;
import com.amumu.netctoss.dao.RoleDao;
import com.amumu.netctoss.entity.Admin;
import com.amumu.netctoss.entity.Role;
/**
 * 管理员模块
 */
public class AdminAction extends BaseAction{
	@Resource
	private AdminDao dao;
	@Resource
	private RoleDao roleDao;
	private List<Admin> adminList;
	private List<Role> roleList;
	private Map<String,String> moduleOptions;
	private Admin admin;
	//密码重置ID
	private Integer[] adminId;
	//搜索条件
	private String module;
	private String roleName;
	
	
	public String findAll(){
		try {
			moduleOptions = dao.getModuleOptions();
			if(module!=null){
				roleName = new String(roleName.getBytes("iso-8859-1"), "utf-8").trim();
			}
			setPageCount(dao.getPageCount(PAGE_SIZE, module, roleName));
			adminList = dao.search(page, PAGE_SIZE, module, roleName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	//转向添加页面
	public String toAdd(){
		try {
			roleList = roleDao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	//添加
	public String save(){
		try {
			dao.save(admin);
			ok = true;
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	//删除
	public String delete(){
		try {
			dao.delete(id);
			ok = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	//转身更新页面
	public String toUpdate(){
		try {
			admin = dao.findById(id);
			roleList = roleDao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	//执行更新
	public String update(){
		try {
			dao.update(admin);
			ok=true;
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//重置密码
	public String resetPwd(){
		try {
			for(int i:adminId) System.out.println(i);
			dao.resetPwd(adminId);
			ok=true;
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	
	//getter and setter
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public List<Role> getRoleList() {
		return roleList;
	}
	public List<Admin> getAdminList() {
		return adminList;
	}
	public void setAdminId(Integer[] adminId) {
		this.adminId = adminId;
	}
	public String getModule() {
		return module;
	}
	public void setModule(String module) {
		this.module = module;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public Map<String, String> getModuleOptions() {
		return moduleOptions;
	}

}
